<template>
	<view class="container">
		<view class="item" v-for="(item, index) in photoList" :key="index">
			<view class="item-top">
				<image class="photo-img" :src="item.showUrl" mode="heightFix"></image>
				<view class="del" @click="handleDelete(index)">
					<image class="del-icon" src="@/static/img/close02.png" mode="aspectFit"></image>
				</view>
				<view class="preview-box" @click="previewImg(index)">
					<image src="@/static/img/eye01.png" mode="aspectFit"></image>
					<text>预览</text>
				</view>
			</view>
			<NumberBox v-model="item.num" :asyncChange="false" @change="changePhotoList" />
		</view>
		<image class="item-add" src="@/static/img/add-photo.png" @click="showUpload = true" />

		<view class="footer safe-btm">
			<!-- <view class="total-price">
				<text class="price-tips">
					合计：
				</text>
				<view class="price">
					￥<span>12666</span>
				</view>
			</view> -->
			<view class="submit-btn" style="margin-left: auto;" @click="navToPayment">
				前往结算
			</view>
		</view>

		<UploadFile v-model="showUpload" type="image" @upload="handleUpload" />
	</view>
</template>

<script>
	import {
		mapGetters
	} from 'vuex'
	import NumberBox from '@/pages/indexs/components/NumberBox.vue'
	import UploadFile from '../components/UploadFile.vue'
	import {
		uploadFile
	} from '@/utils/util'
	export default {
		components: {
			NumberBox,
			UploadFile,
		},
		data() {
			return {
				// 展示上传组件
				showUpload: false,
				id: 0,
			}
		},
		computed: {
			...mapGetters(['photoList']),
		},
		onLoad({
			title,
			id
		}) {
			if (!title)
				return
			this.id = id
			uni.setNavigationBarTitle({
				title
			})
		},
		methods: {
			navToPayment() {
				if (!this.photoList.length)
					return this.$showToast('请先上传相片')
				this.$navTo('pages/indexs/photo_payment', {
					orderType: 2,
					imageSize: this.id
				})
			},
			// 处理选中文件
			async handleUpload(e) {
				try {
					this.$showLoading()
					let fileList = []
					for (const item of e.fileList) {
						const res = await uploadFile(item, false)
						fileList.push({
							...res.result,
							num: 1
						})
					}
					this.$store.commit('photo/updatePhotoList', this.photoList.concat(fileList))
					this.showUpload = false
					uni.hideLoading()
					if (e.errNum) this.$showToast(`有${e.errNum}个图片上传失败`)
				} catch (err) {
					console.log(err);
				}
			},
			/**
			 * 删除
			 */
			handleDelete(index) {
				let dataList = JSON.parse(JSON.stringify(this.photoList))
				dataList.splice(index, 1)
				this.$store.commit('photo/updatePhotoList', dataList)
			},
			/**
			 * 更改数据
			 */
			changePhotoList() {
				this.$store.commit('photo/updatePhotoList', this.photoList)
			},
			previewImg(current) {
				uni.previewImage({
					urls: this.photoList.map(item => item.showUrl),
					current
				})
			},
		}
	}
</script>

<style>
	page {
		background-color: #fff;
	}
</style>
<style lang="scss" scoped>
	.container {
		--footer-height: 110rpx;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		padding: 20rpx;
		padding-bottom: calc(var(--footer-height) + 20rpx + constant(safe-area-inset-bottom));
		padding-bottom: calc(var(--footer-height) + 20rpx + env(safe-area-inset-bottom));
	}

	.item {
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 40rpx;
		width: 346rpx;
	}

	.item-top {
		position: relative;
		margin-bottom: 30rpx;
		width: 100%;
		height: 460rpx;
		border-radius: 10rpx;
		text-align: center;
		overflow: hidden;
	}

	.photo-img {
		width: 100%;
		height: 100%;
	}

	.del {
		position: absolute;
		top: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 52rpx;
		height: 52rpx;
		border-radius: 0 10rpx 0 10rpx;
		background: rgba(0, 0, 0, 0.6);

		.del-icon {
			width: 36rpx;
			height: 36rpx;
		}
	}

	.preview-box {
		position: absolute;
		bottom: 0;
		left: 0;
		right: 0;
		display: flex;
		justify-content: center;
		align-items: center;
		width: 100%;
		height: 60rpx;
		border-radius: 0 0 10rpx 10rpx;
		background: rgba(0, 0, 0, 0.6);

		image {
			width: 26rpx;
			height: 26rpx;
		}

		text {
			margin-left: 6rpx;
			color: #ffffff;
			font-size: 24rpx;
		}
	}

	.item-add {
		width: 346rpx;
		height: 460rpx;
	}

	.footer {
		position: fixed;
		right: 0;
		bottom: 0;
		left: 0;
		display: flex;
		justify-content: right;
		align-items: center;
		padding-left: 30rpx;
		padding-right: 30rpx;
		height: var(--footer-height);
		border-top: 2rpx solid #f4f4f4;
		background-color: #fff;
		z-index: 999;
	}

	.submit-btn {
		margin-left: 20rpx;
		width: 228rpx;
		height: 70rpx;
		line-height: 70rpx;
		border-radius: 35rpx;
		background: linear-gradient(135deg, #ffb300 4%, #ff6a00 100%);
		color: #ffffff;
		text-align: center;
		font-size: 24rpx;
	}

	.total-price {
		display: flex;
		align-items: flex-end;
		margin-left: auto;
		font-size: 20rpx;

		.price-tips {
			color: #606060;
		}

		.price {
			color: $theme-color;

			span {
				font-size: 36rpx;
				line-height: 100%;
			}
		}
	}
</style>